package com.fenghongzhang.day017;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText input;
    private Button btn;

    private static final String TAG = "MainActivity";
    private List<Integer> list;

    private List<Music> music ;

    private List<MusicBean> beans;
    private String url = "http://musicapi.leanapp.cn/search/suggest?keywords=";
    private String url2 = "http://musicapi.leanapp.cn/music/url?id=";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = (EditText) findViewById(R.id.input);
        btn = (Button) findViewById(R.id.btn);
        list = new ArrayList<>();
        music = new ArrayList<>();
        beans = new ArrayList<>();



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = input.getText().toString();

                new MyTask().execute(url+s);
            }
        });

    }



    class MyTask2 extends AsyncTask<String,Void,String>{


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Gson gson = new Gson();
            Music music = gson.fromJson(s, Music.class);
            List<Music.DataBean> data = music.getData();
            for (int i = 0; i < data.size(); i++) {

                Music.DataBean dataBean = data.get(i);

                String url = dataBean.getUrl();

                for (int j = 0; j < beans.size(); j++) {
                    MusicBean musicBean = beans.get(i);
                    if(dataBean.getId()== musicBean.getId()){
                        musicBean.setUrl(url);
                    }

                }

                Log.i(TAG, "onPostExecute: "+url);
            }
//            Log.i(TAG, "onPostExecute222: "+s);

            for (int i = 0; i < beans.size(); i++) {
                Log.i(TAG, "onPostExecute: "+beans.get(i).getId());
                Log.i(TAG, "onPostExecute: "+beans.get(i).getTitle());
                Log.i(TAG, "onPostExecute: "+beans.get(i).getUrl());

            }
        }

        @Override
        protected String doInBackground(String... strings) { //子线程
            try {
                URL url = new URL(strings[0]);

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                if (connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    StringBuilder stringBuilder = new StringBuilder();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while((len = inputStream.read(bytes)) != -1){
                        stringBuilder.append(new String(bytes,0,len));
                    }

                    return stringBuilder.toString();
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }


    class MyTask extends AsyncTask<String,Void,String>{


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Gson gson = new Gson();
            MusicList musicList = gson.fromJson(s, MusicList.class);
            Log.i(TAG, "onPostExecute: "+s);
            List<MusicList.ResultBean.SongsBean> songs = musicList.getResult().getSongs();
            for (int i = 0; i < songs.size(); i++) {
                MusicBean musicBean = new MusicBean();

                MusicList.ResultBean.SongsBean songsBean = songs.get(i);
                int id = songsBean.getId(); //用它去歌曲的url
                list.add(id);

                musicBean.setTitle(songsBean.getName());
                musicBean.setId(songsBean.getId());

                new MyTask2().execute(url2+id);
                beans.add(musicBean);
            }
            Log.i(TAG, "onPostExecute:11 "+list.size());


        }

        @Override
        protected String doInBackground(String... strings) { //子线程
//            http://musicapi.leanapp.cn/search/suggest?keywords=邓紫棋
            try {
                URL url = new URL(strings[0]);

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                if (connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    StringBuilder stringBuilder = new StringBuilder();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while((len = inputStream.read(bytes)) != -1){
                        stringBuilder.append(new String(bytes,0,len));

                    }

                    return stringBuilder.toString();
                }




            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }
}




